package com.pjedu.nbmalluser.service.imp;

import com.pjedu.nbmallcommon.utils.MD5Util;
import com.pjedu.nbmalluser.entity.MallAdminUser;
import com.pjedu.nbmalluser.mapper.MallAdminUserMapper;
import com.pjedu.nbmalluser.service.MallAdminUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author 汪洋
 * @Description
 */
@Service
public class MallAdminUserServiceImp implements MallAdminUserService {
    @Resource
    private MallAdminUserMapper mallAdminUserMapper;
    @Override
    public MallAdminUser login(MallAdminUser user) {
        return mallAdminUserMapper.selectOne(user);
    }

    @Override
    public Boolean updatePassword(Integer loginUserId, String originalPassword, String newPassword) {
        MallAdminUser adminUser = mallAdminUserMapper.selectByPrimaryKey(loginUserId);
        //当前用户非空才可以进行更改
        if (adminUser != null) {
            String originalPasswordMd5 = MD5Util.MD5Encode(originalPassword, "UTF-8");
            String newPasswordMd5 = MD5Util.MD5Encode(newPassword, "UTF-8");
            //比较原密码是否正确
            if (originalPasswordMd5.equals(adminUser.getLoginPassword())) {
                //设置新密码并修改
                adminUser.setLoginPassword(newPasswordMd5);
                if (mallAdminUserMapper.updateByPrimaryKeySelective(adminUser) > 0) {
                    //修改成功则返回true
                    return true;
                }
            }
        }
        return false;
    }
}
